﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core;

namespace Web.Controllers
{
    public class problemController : JsonController
    {
        //
        // GET: /problem/

        public ActionResult getProblemList(int start, int limit)
        {
            return Json(problem.getProblemList(start, limit, Certify.username, Certify.god));
        }

        public ActionResult getPIDPoint(int pid)
        {
            return Json(problem.getPIDPoint(pid, Certify.god));
        }

        [OnlyGod]
        public ActionResult deleteProblem(int pid)
        {
            return Json(problem.deleteProblem(pid));
        }

        public ActionResult showProblem(int pid)
        {
            Sql.ProblemStatistics ps;
            var p = problem.showProblem(pid, out ps, Certify.god);
            if (p == null) return Content("题目不存在");
            ViewData["ps"] = ps;
            return View(p);
        }

        [OnlyGod]
        public ActionResult cleanProblemStatus(int pid)
        {
            return Json(problem.cleanProblemStatus(pid));
        }

        [OnlyGod]
        public ActionResult rejudgeProblemStatus(int pid)
        {
            return Json(problem.rejudgeProblemStatus(pid));
        }

        [OnlyGod]
        public ActionResult getProblemCompleteInfo(int pid)
        {
            return Json(problem.getProblemCompleteInfo(pid));
        }

        [ValidateInput(false)]
        [OnlyGod]
        public ActionResult addOrEditProblem(int? oldPid, int pid, string title, int timeLimit, int memoryLimit, string disabled,
            string description, string input, string output, string sampleInput, string sampleOutput, string hint, string source,
            int? exampleLang, string exampleCode, string inputData, string outputData,
            string uploadDataFile, HttpPostedFileBase inputDataFile, HttpPostedFileBase outputDataFile,
            string specialJudge, int? specialLang, string specialCode)
        {
            IsFileUpload = true;
            CheckEmpty(title, description, input, output, sampleInput, sampleOutput);
            if (exampleLang != null) CheckEmpty(exampleCode);
            var _uploadDataFile = uploadDataFile.ToBool();
            var _specialJudge = specialJudge.ToBool();
            if (_uploadDataFile)
            {
                CheckNull(inputDataFile, outputDataFile);
                inputData = new System.IO.StreamReader(inputDataFile.InputStream).ReadToEnd();
                outputData = new System.IO.StreamReader(outputDataFile.InputStream).ReadToEnd();
            }
            else
            {
                CheckEmpty(inputData, outputData);
                if (_specialJudge) CheckEmpty(specialLang, specialCode);
            }
            if (!inputData.EndsWith("\n")) inputData += "\r\n";
            if (!outputData.EndsWith("\n")) outputData += "\r\n";

            return Json(problem.addOrEditProblem(oldPid, pid, title, timeLimit, memoryLimit, disabled.ToBool(),
                description.TrimHtmlEnd(), input.TrimHtmlEnd(), output.TrimHtmlEnd(), sampleInput, sampleOutput, hint.TrimHtmlEnd().ToHtmlQ(), source.ToQ(),
                exampleLang, exampleCode, inputData, outputData, _specialJudge, specialLang, specialCode));
        }

        [OnlyGod]
        public ActionResult getAutoPid()
        {
            return Json(problem.getAutoPid());
        }

        public ActionResult getProblemStatistics(int pid)
        {
            return Json(problem.getProblemStatistics(pid));
        }

        public ActionResult getProblemRanklist(int pid, int start, int limit)
        {
            return Json(problem.getProblemRanklist(pid, start, limit, Certify.username));
        }

    }
}
